package com.spring.shops.service.impl;

import com.spring.shops.dao.TzCategoryMapper;
import com.spring.shops.model.entity.TzCategory;
import com.spring.shops.model.vo.CategoryVo;
import com.spring.shops.service.CategoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Resource
    private TzCategoryMapper tzCategoryMapper;

    @Override
    public TzCategory[] tableSearch(){
        TzCategory[] tzCategories = tzCategoryMapper.selectAll();
        return tzCategories;
    }

    @Override
    public int save(CategoryVo vo){
        TzCategory tzCategory = new TzCategory();
        tzCategory.setCategoryId(vo.getCategoryId());
        tzCategory.setCategoryName(vo.getCategoryName());
        tzCategory.setPic(vo.getPic());
        tzCategory.setParentId(vo.getParentId());
        tzCategory.setGrade(vo.getGrade());
        tzCategory.setSeq(vo.getSeq());
        System.out.println(tzCategory.getSeq());
        tzCategory.setIcon(vo.getIcon());
        tzCategory.setShopId(1L);
        tzCategory.setStatus(vo.getStatus());
        tzCategory.setRecTime(new Date());
        int i = tzCategoryMapper.insertSelective(tzCategory);
        return i;
    }

    @Override
    public boolean delete(long id){
        int num=tzCategoryMapper.selectParentId(id);
        if (num>0){
            return false;
        }else {
            tzCategoryMapper.deleteByPrimaryKey(id);
            return true;
        }
    }

    @Override
    public TzCategory info(long id){
        TzCategory tzCategory = tzCategoryMapper.selectByPrimaryKey(id);
        return tzCategory;
    }
    @Override
    public int update(TzCategory tzCategory){
        int i = tzCategoryMapper.updateByPrimaryKeySelective(tzCategory);
        return i;
    }

}
